<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Failed;
use App\Admin\Extensions\Success;
use App\Http\Controllers\Controller;
use App\Models\Recharge;
use App\Models\User;
use App\Models\Wallet;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Widgets\InfoBox;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RechargeLogController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('充值记录')
            ->description('充值记录')
            ->row(function (Row $row) {
                $row->column(4, function (Column $column) {
                    $total = Recharge::where('status', 1)->sum('money');
                    $infoBox = new InfoBox('充值总金额', 'calendar', 'aqua', '/admin/rechargeLog', $total);
                    $column->append($infoBox);
                });
                $row->column(4, function (Column $column) {
                    $total = Recharge::where('status', 1)->where('type', 1)->sum('money');
                    $infoBox = new InfoBox('微信充值', 'calendar', 'aqua', '/admin/rechargeLog?type=1', $total);
                    $column->append($infoBox);
                });
                $row->column(4, function (Column $column) {
                    $total = Recharge::where('status', 1)->where('type',2)->sum('money');
                    $infoBox = new InfoBox('支付宝充值', 'calendar', 'aqua', '/admin/rechargeLog?type=2', $total);
                    $column->append($infoBox);
                });

            })
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Recharge);

        $grid->model()->where('status', 1);
        $grid->id('Id');
        $grid->user_id('用户昵称')->display(function ($value) {
            $user = User::where('id', $value)->value('nickname');
            if ($user) {
                return $user;
            }
        });
        $grid->money('充值金额');
        $grid->type('充值渠道')->display(function ($value) {
            switch ($value) {
                case '0':
                    return "<span class='label label-success'>人工充值</span>";
                    break;
                case '1':
                    return "<span class='label label-success'>微信</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>支付宝</span>";
                    break;

                default:
                    # code...
                    break;
            }
        });
        $grid->status('充值状态')->display(function ($value) {
            switch ($value) {
                case '0':
                    return "<span class='label label-warning'>待审核</span>";
                    break;
                case '1':
                    return "<span class='label label-success'>已通过</span>";
                    break;
                case '2':
                    return "<span class='label label-primary'>已取消</span>";
                    break;
                case '3':
                    return "<span class='label label-danger'>未通过</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');

        $grid->disableActions();
        // $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('status', '充值状态')->radio([
                0 => '待审核',
                1 => '已通过',
                2 => '已取消',
                3 => '未通过',
            ]);
            $filter->equal('type', '充值渠道')->radio([
                0 => '人工',
                1 => '微信',
                2 => '支付宝',
            ]);
            // $filter->scope('待审核')->where('status', 0);
            $filter->scope('今日新增', '今日新增')->whereDate('created_at', date('Y-m-d'));

        });
        $grid->footer(function ($query) {

            // 查询出已支付状态的订单总金额
            $data = $query->where('status', 1)->sum('money');

            return "<div style='padding: 10px;'>当前总充值 ： $data</div>";
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Recharge::findOrFail($id));

        $show->id('Id');
        $show->user_id('User id');
        $show->money('Money');
        $show->type('Type');
        $show->status('Status');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Recharge);

        $form->number('user_id', 'User id');
        $form->decimal('money', 'Money');
        $form->number('type', 'Type');
        $form->number('status', 'Status');

        return $form;
    }

    //审核通过
    public function success(Request $request, Wallet $wallet)
    {
        $id = $request->id;
        $recharge = Recharge::findOrFail($id);

        $wallet = Wallet::where('user_id', $recharge->user_id)->first();

        DB::beginTransaction();
        try {
            $recharge->status = 1;
            $wallet->money = bcadd($wallet->money, $recharge->money, 2);
            $wallet->save();
            $recharge->save();

            DB::commit();

            return $this->ajax_success('操作成功');

        } catch (\Exception $exception) {

            DB::rollBack();

            return $this->ajax_error('操作失败');

        }

    }
    //审核不通过
    public function failed(Request $request)
    {
        $id = $request->id;
        $recharge = Recharge::findOrFail($id);
        $recharge->status = 3;
        $recharge->comment = $request->comment;
        $res = $recharge->save();
        if ($res) {
            return $this->ajax_success('操作成功');
        } else {
            return $this->ajax_error('操作失败');
        }

    }

}
